package com.start.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.GoodsDao;
import com.start.manager.domain.entity.Goods;
import com.start.manager.domain.vo.GoodsVO;
import com.start.manager.domain.vo.OptionsVO;
import com.start.manager.service.IGoodsService;
import com.start.manager.util.MyUtil;
import com.start.manager.util.PageUtil;
import com.start.manager.util.RedisUtil;
import com.start.manager.util.constant.RedisKeyConstant;
import com.start.manager.util.constant.StatusConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements IGoodsService {

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public PageUtil findList(GoodsVO goodsVO) {
        QueryWrapper<Goods> queryWrapper = initQueryWrapper(goodsVO);
        if (goodsVO.getSize() == 0){
            //获取全部的数据
            List<Goods> list = this.baseMapper.selectList(queryWrapper);
            return new PageUtil(list.size(),0,list);
        }
        //分页查询货物详情
        Page<Goods> page = new Page<>(goodsVO.getPage(),goodsVO.getSize());
        queryWrapper.orderByAsc("id");
        Page<Goods> pageData = this.baseMapper.selectPage(page, queryWrapper);
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),pageData.getRecords());
    }

    @Override
    public boolean saveGoods(GoodsVO goodsVo) {
        Goods goods = new Goods();
        if (StringUtils.isBlank(goodsVo.getId())) {
            goods.setId(myUtil.generateId("G"));
        }
        BeanUtils.copyProperties(goodsVo,goods);
        int insert = this.baseMapper.insert(goods);
        return insert > 0 ? true : false;
    }

    @Override
    public List<Goods> getOptionsList(OptionsVO optionsVO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",optionsVO.getType()).eq("status", StatusConstant.USE);
        queryWrapper.orderByAsc("id");
        return this.baseMapper.selectList(queryWrapper);

    }

    @Override
    public String getRedis(String goodsId) {
        if (StringUtils.isBlank(goodsId)){
            return "未知";
        }
        String value = (String) redisUtil.hget(RedisKeyConstant.GOODS, goodsId);
        if (StringUtils.isNotBlank(value)) {
            Goods goods = JSONObject.parseObject(value).toJavaObject(Goods.class);
            return goods.getName();
        }
        Goods goods = this.baseMapper.selectById(goodsId);
        if (Objects.isNull(goods)){
            return "未知";
        }
        redisUtil.hset(RedisKeyConstant.COMPANY,goodsId, JSONObject.toJSONString(goods));
        return goods.getName();

    }

    private QueryWrapper initQueryWrapper(GoodsVO goodsVO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(goodsVO.getName())){
            queryWrapper.like("name",goodsVO.getName());
        }
        if (StringUtils.isNotBlank(goodsVO.getType())){
            queryWrapper.eq("type",goodsVO.getType());
        }
        if (StringUtils.isNotBlank(goodsVO.getId())){
            queryWrapper.like("id",goodsVO.getId());
        }
        if (!Objects.isNull(goodsVO.isStatus())){
            queryWrapper.eq("status",goodsVO.isStatus());
        }
        return queryWrapper;
    }
}
